﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.BizTalk.Streaming;
using Microsoft.BizTalk.Message.Interop;
using Microsoft.BizTalk.Operations;
using Microsoft.BizTalk.Edi.Shared;
using Microsoft.XLANGs.BaseTypes;

namespace Locallab.Ack999Helper.BusinessContextReader
{
    public class BusinessContextReaderFactory
    {
        private FileHeaderData delimiter;
        StreamReader reader;
        string strFileName;

        public FileHeaderData FileHeader { get { return delimiter; } }

        public BusinessContextReaderFactory(string strFileName)
        {
            delimiter = new FileHeaderData();
            this.strFileName = strFileName;            
        }

        public void ProcessStream(ReadOnlySeekableStream x12MessageStream)
        {
            if (ReadHeader(x12MessageStream))
                ProcessX12Stream(x12MessageStream);
        }

        private bool ReadHeader(ReadOnlySeekableStream x12MessageStream)
        {
            var bResult = false;            
            var buffer = new char[250];

            reader = new StreamReader(x12MessageStream, true);
            reader.Read(buffer, 0, 250);            
            var strData = new string(buffer, 0, 250);
            strData = strData.Trim();

            reader.DiscardBufferedData();
            x12MessageStream.Seek(0, SeekOrigin.Begin);
                        
            var delimiterReader = new FileHeaderDataReader(strData);

            bResult = delimiterReader.ValidateHeader();

            if(bResult)
            {
                delimiterReader.Delimiter.FileName = this.strFileName;
                delimiterReader.Delimiter.Sender = "Aplollo Hospital";
                delimiterReader.Delimiter.Receiver = "WCG";
                delimiterReader.SaveFileHeaderInformation();
                this.delimiter = delimiterReader.Delimiter;
            }

            return bResult;
        }


        private void ProcessX12Stream(ReadOnlySeekableStream x12MessageStream)
        {
            var x12Reader = GetReader();

            if (x12Reader == null)
                return;

            x12Reader.ProcessX12Stream(this.delimiter, x12MessageStream,this.reader);

        }

        private IX12Reader GetReader()
        {
            switch (this.delimiter.Version)
            {
                case "005010X222":
                case "005010X222E1":
                case "005010X222A1":
                case "005010X223":
                case "005010X223A1":
                case "005010X223E1":
                case "005010X223A2":
                case "005010X224":
                case "005010X224A1":
                case "005010X224E1":
                case "005010X224A2":
                case "004010X098A1":
                    return new X12837Reader();
                default:
                    return null;
            }
        }
    }
}
